package com.ruoyi.system.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.aid.AidAllocation;
import com.ruoyi.system.service.aid.IAidAllocationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物资分配Controller
 */
@RestController
@RequestMapping("/aid/allocation")
public class AidAllocationController extends BaseController
{
    @Autowired
    private IAidAllocationService aidAllocationService;

    /**
     * 查询物资分配列表
     */
    @PreAuthorize("@ss.hasPermi('aid:allocation:list')")
    @GetMapping("/list")
    public TableDataInfo list(AidAllocation aidAllocation)
    {
        startPage();
        List<AidAllocation> list = aidAllocationService.selectAidAllocationList(aidAllocation);
        return getDataTable(list);
    }

    /**
     * 导出物资分配列表
     */
    @PreAuthorize("@ss.hasPermi('aid:allocation:export')")
    @Log(title = "物资分配", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AidAllocation aidAllocation)
    {
        List<AidAllocation> list = aidAllocationService.selectAidAllocationList(aidAllocation);
        ExcelUtil<AidAllocation> util = new ExcelUtil<AidAllocation>(AidAllocation.class);
        return util.exportExcel(list, "allocation");
    }

    /**
     * 获取物资分配详细信息
     */
    @PreAuthorize("@ss.hasPermi('aid:allocation:query')")
    @GetMapping(value = "/{allocationId}")
    public AjaxResult getInfo(@PathVariable("allocationId") Long allocationId)
    {
        return success(aidAllocationService.selectAidAllocationById(allocationId));
    }

    /**
     * 新增物资分配
     */
    @PreAuthorize("@ss.hasPermi('aid:allocation:add')")
    @Log(title = "物资分配", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AidAllocation aidAllocation)
    {
        return toAjax(aidAllocationService.insertAidAllocation(aidAllocation));
    }

    /**
     * 修改物资分配
     */
    @PreAuthorize("@ss.hasPermi('aid:allocation:edit')")
    @Log(title = "物资分配", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AidAllocation aidAllocation)
    {
        return toAjax(aidAllocationService.updateAidAllocation(aidAllocation));
    }

    /**
     * 删除物资分配
     */
    @PreAuthorize("@ss.hasPermi('aid:allocation:remove')")
    @Log(title = "物资分配", businessType = BusinessType.DELETE)
    @DeleteMapping("/{allocationIds}")
    public AjaxResult remove(@PathVariable Long[] allocationIds)
    {
        return toAjax(aidAllocationService.deleteAidAllocationByIds(allocationIds));
    }

    /**
     * 审核物资分配
     */
    @PreAuthorize("@ss.hasPermi('aid:allocation:verify')")
    @Log(title = "物资分配", businessType = BusinessType.UPDATE)
    @PutMapping("/verify")
    public AjaxResult verify(@RequestBody AidAllocation aidAllocation)
    {
        return toAjax(aidAllocationService.verifyAidAllocation(aidAllocation));
    }

    /**
     * 根据捐赠ID查询分配记录
     */
    @PreAuthorize("@ss.hasPermi('aid:allocation:list')")
    @GetMapping("/donation/{donationId}")
    public TableDataInfo listByDonation(@PathVariable("donationId") Long donationId)
    {
        startPage();
        List<AidAllocation> list = aidAllocationService.selectAidAllocationByDonationId(donationId);
        return getDataTable(list);
    }

    /**
     * 根据需求ID查询分配记录
     */
    @PreAuthorize("@ss.hasPermi('aid:allocation:list')")
    @GetMapping("/demand/{demandId}")
    public TableDataInfo listByDemand(@PathVariable("demandId") Long demandId)
    {
        startPage();
        List<AidAllocation> list = aidAllocationService.selectAidAllocationByDemandId(demandId);
        return getDataTable(list);
    }
}